package com.yc.qunxing.web;


import com.yc.qunxing.dao.SensitiveWordRepository;
import com.yc.qunxing.entity.Comment;
import com.yc.qunxing.entity.SensitiveWord;
import com.yc.qunxing.entity.Users;
import com.yc.qunxing.result.Result;
import com.yc.qunxing.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users/comment")
@Slf4j
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private SensitiveWordRepository sensitiveWordRepository;

    @PostMapping("addComment")
    public Result AddComment(@RequestBody Comment comment){

        Comment savedComment=commentService.saveComment(comment);
        return Result.success(savedComment);
    }
    @GetMapping("findCommentByDriverId")
    public Result<List<Comment>> FindCommentByDriverId(@RequestParam Integer driverId){
        List<Comment> list= commentService.findCommentByDriverId(driverId);
        return Result.success(list);
    }
    @PostMapping("/incrementLike")
    public Result incrementLike(@RequestBody Map<String, String> request) {
        String commentId = request.get("commentId");
        commentService.updateCommentLikenum(commentId, 1);
        return Result.success("点赞成功");
    }

    @PostMapping("/decrementLike")
    public Result decrementLike(@RequestBody Map<String, String> request) {
        String commentId = request.get("commentId");
        commentService.updateCommentLikenum(commentId, -1);
        return Result.success("取消点赞成功");
    }
    @PostMapping("/addSensitiveWord")
    public Result AddSensitiveWord(@RequestBody SensitiveWord sensitiveWord){
        SensitiveWord existingWord = sensitiveWordRepository.findByWord(sensitiveWord.getWord());
        if (existingWord != null) {
            return Result.error("该敏感词已存在");
        }
        sensitiveWordRepository.save(sensitiveWord);
        return Result.success("添加敏感词成功");
    }
}
